﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Sautom.ClientComunication;
using Sautom.ClientComunication.Events;

namespace Sautom.Modules.Client.ViewModels
{
    public class ModuleTaskButtonViewModel : NotificationObject
    {
        #region Constructor
        public ModuleTaskButtonViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            RegionManager = regionManager;
            EventAggregator = eventAggregator;
            this.Initialize();
        }

        protected IRegionManager RegionManager { get; set; }
        public IEventAggregator EventAggregator { get; set; }

        private void Initialize()
        {
            this.ShowModuleViewCommand = new DelegateCommand(ShowModuleViewCommandExecute);
            this.IsChecked = false;

            EventAggregator.GetEvent<NavigationCompletedEvent>().Subscribe((path) =>
                                                                               {
                                                                                   this.IsChecked = path ==
                                                                                                    PathProvider.
                                                                                                        ClientIndex;
                                                                               });
        }

        #endregion

        #region Command Properties
        public DelegateCommand ShowModuleViewCommand { get; set; }
        #endregion

        #region Properties

        private bool? _pIsChecked;
        public bool? IsChecked
        {
            get { return _pIsChecked; }

            set
            {
                base.RaisePropertyChanged(() => IsChecked);
                _pIsChecked = value;
                base.RaisePropertyChanged(() => IsChecked);
            }
        }

        #endregion

        #region Commands
        private void ShowModuleViewCommandExecute()
        {
            RegionManager.RequestNavigate(RegionProvider.MainRegion, PathProvider.ClientIndex, NavigationCompleted);
        }

        private void NavigationCompleted(NavigationResult result)
        {
            // Exit if navigation was not successful
            if (result.Result != true) return;

            // Publish Event
            EventAggregator.GetEvent<NavigationCompletedEvent>()
                .Publish(PathProvider.ClientIndex);
        }

        #endregion
    }
}
